import { ValidatorFn, FormGroup } from '@angular/forms'

export interface IValidatorErrors {
  required?: string,
  minlength?: string,
  maxlength?: string,
  pattern?: string,
}

export interface IFieldOptions<T> {
  value?: T,
  key?: string,
  label?: string,
  disabled?: boolean,
  validators?: ValidatorFn[],
  validatorErrors?: IValidatorErrors,
  order?: number,
  controlType?: string,
  onModelChange?: (value: any, form: FormGroup) => void
  [key: string]: any,
}


export class FieldBase<T> {
  value: T
  key: string
  label: string
  disabled = false
  validators: ValidatorFn[]
  validatorErrors: IValidatorErrors
  order: number
  controlType: string
  span: number
  show: any
  fields: any
  onModelChange: (value: any, form: FormGroup) => void

  constructor(options: IFieldOptions<T> = {}) {
    this.value = options.value
    this.key = options.key || ''
    this.label = options.label || ''
    this.validators = options.validators
    this.validatorErrors = options.validatorErrors || {}
    this.order = options.order === undefined ? 1 : options.order
    this.controlType = options.controlType || ''
    this.onModelChange = options.onModelChange
    this.disabled = options.disabled
  }
}
